package com.commbizobj.category;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Represents a set of categories' parent-child relationship.
 * 
 * @author Otacon
 *
 */
public class CategoryTree {
	private Category root;
	private Map<Category, List<Category>> tree;
	
	// -- Public methods --
	public boolean hasChildren(Category c){
		List<Category> childrenList = getChildren(c);
		return childrenList != null && childrenList.size() > 0;
	}
	public List<Category> getChildren(Category parent){
		return getTree().get(parent);
	}
	public Category getParent(Category child){
		for(Category p : getTree().keySet()){
			List<Category> childrenList = getTree().get(p);
			if(childrenList.contains(child)){
				return p;
			}
		}
		return null;
	}
	
	// -- Getters and setters --
	public Category getRoot() {
		return root;
	}
	public void setRoot(Category root) {
		this.root = root;
	}
	public void setTree(Map<Category, List<Category>> tree) {
		this.tree = tree;
	}
	public Map<Category, List<Category>> getTree() {
		if(tree == null){
			tree = new HashMap<Category, List<Category>>();
		}
		return tree;
	}
	
}
